package com.tyd.cams;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.tyd.cams.Adapter.FanKanRecyclerAdapter;
import com.tyd.cams.Adapter.ZycjStuListAdapter;
import com.tyd.cams.entity.FanKabean;
import com.tyd.cams.entity.ZycxStuListBeam;
import com.tyd.cams.utils.SPUtils;
import com.xuexiang.xui.widget.dialog.DialogLoader;
import com.xuexiang.xui.widget.dialog.materialdialog.MaterialDialog;
import com.xuexiang.xui.widget.dialog.strategy.IDialogStrategy;
import com.xuexiang.xui.widget.dialog.strategy.InputCallback;
import com.xuexiang.xui.widget.dialog.strategy.InputInfo;
import com.xuexiang.xui.widget.dialog.strategy.impl.MaterialDialogStrategy;
import com.xuexiang.xui.widget.layout.XUIButton;
import com.xuexiang.xui.widget.toast.XToast;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.HashMap;
import java.util.List;

import okhttp3.Call;

import static com.xuexiang.xui.XUI.getContext;

public class StufankaActivity extends AppCompatActivity {
    RecyclerView recyclerView;
    FanKanRecyclerAdapter adapter;
    LinearLayout lin_stu;
    TextView card_num, card_name, card_yu_e, lei_xin, card_ztai;
    XUIButton btn_cz;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_stufanka);
        lin_stu = findViewById(R.id.lin_stu);
        recyclerView = findViewById(R.id.fanka_recycleview);
        if (SPUtils.getUsertype().equals("1")) {
            lin_stu.setVisibility(View.GONE);
            getZycjList();
        } else {
            recyclerView.setVisibility(View.GONE);
            getStuFankaInfo(SPUtils.getaccount());
            btn_cz = findViewById(R.id.btn_cz);
            btn_cz.setOnClickListener(v -> {
                IDialogStrategy mDialogLoader = DialogLoader.getInstance().setIDialogStrategy(new MaterialDialogStrategy());

                InputInfo inputInfo = new InputInfo(InputType.TYPE_CLASS_TEXT);
                inputInfo.setHint(" 请输入充值金额");

                mDialogLoader.showInputDialog(StufankaActivity.this
                        , R.drawable.fankacz,
                        "饭卡充值", "请在输入框中输入需要充值金额", inputInfo,
                        new InputCallback() {
                            @Override
                            public void onInput(@NonNull DialogInterface dialog, CharSequence input) {
//                                XToast.normal(StufankaActivity.this,input).show();
                            }
                        }, "确认", (dialogInterface, i) -> {
                            String input = ((MaterialDialog) dialogInterface).getInputEditText().getText().toString();//获取输入金额
                            card_yu_e=findViewById(R.id.card_yu_e);
                            String cz = card_yu_e.getText().toString();//获取当前余额
                            Integer jinE= Double.valueOf(cz).intValue()+Integer.valueOf(input);//更新后余额，丢给数据库更新
                            getStuFKcz(jinE.toString());
                          card_yu_e.setText(jinE.toString());
                            dialogInterface.dismiss();
                        }, "取消", (dialogInterface, i) -> {
                            XToast.normal(StufankaActivity.this, "取消充值").show();
                            dialogInterface.cancel();
                        });
            });
        }


    }
    /*        学生饭卡充值*/
    private void getStuFKcz(String jine) {

        new Thread(() -> OkHttpUtils.post()
                .url(SPUtils.getIP() + "/foodcard/mgetStuCz")
                .addHeader("Authorization", SPUtils.getToken())
                .addParams("jinE", jine)
                .addParams("Pid", SPUtils.getaccount())
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Toast.makeText(StufankaActivity.this, "学生充值服务器超时", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Log.i("获取的返回信息", response);
                        Gson son = new Gson();
                        HashMap hashMap = son.fromJson(response, HashMap.class);
                        if (hashMap.get("code").toString().equals("200")) {
                            XToast.normal(StufankaActivity.this, "充值成功").show();

                        }else {
                            XToast.normal(StufankaActivity.this, "充值失败").show();
                        }
                    }
                })).start();

    }

    /*        学生饭卡信息*/
    private void getStuFankaInfo(String getaccount) {

        new Thread(() -> OkHttpUtils.post()
                .url(SPUtils.getIP() + "/foodcard/mgetStuInfo")
                .addHeader("Authorization", SPUtils.getToken())
                .addParams("Pid", getaccount)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Toast.makeText(StufankaActivity.this, "学生饭卡链接服务器超时", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Log.i("获取的返回信息", response);
                        Gson son = new Gson();
                        FanKabean fanKabeans = son.fromJson(response, FanKabean.class);
                        card_num = findViewById(R.id.card_num);
                        card_name = findViewById(R.id.card_name);
                        card_yu_e = findViewById(R.id.card_yu_e);
                        lei_xin = findViewById(R.id.lei_xin);
                        card_ztai = findViewById(R.id.card_ztai);
                        card_num.setText(fanKabeans.getCardNum());
                        card_name.setText("姓名：" + fanKabeans.getPname());
                        card_yu_e.setText( fanKabeans.getYuE().toString());
                        lei_xin.setText("学生卡");
                        if (fanKabeans.getCardStatus().equals("1")) {
                            card_ztai.setText("已经停用");
                        } else {
                            card_ztai.setText("正常");
                        }
                        Log.i("饭卡信息", fanKabeans.toString());
                        //设置适配器
//                        Toast.makeText(StufankaActivity.this, fanKabeans.getPname(),Toast.LENGTH_LONG).show();
                    }
                })).start();

    }

    /*教师饭卡查询*/
    private void getZycjList() {
        new Thread(() -> OkHttpUtils.post()
                .url(SPUtils.getIP() + "/foodcard/mgetFKInfo")
                .addHeader("Authorization", SPUtils.getToken())
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Toast.makeText(StufankaActivity.this, "学生查询链接服务器超时", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Log.i("获取的返回信息", response);
                        Gson son = new Gson();
                        List<FanKabean> fanKabeans = son.fromJson(response, new TypeToken<List<FanKabean>>() {
                        }.getType());
                        Log.i("饭卡信息", fanKabeans.toString());
                        //设置适配器
                        recyclerView = findViewById(R.id.fanka_recycleview);
                        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
                        adapter = new FanKanRecyclerAdapter(StufankaActivity.this, fanKabeans);
                        recyclerView.setLayoutManager(new LinearLayoutManager(StufankaActivity.this, LinearLayoutManager.VERTICAL, false));
                        recyclerView.setAdapter(adapter);
                    }
                })).start();

    }
}